package com.wnxy.replies.controller;

import com.wnxy.article.api.ArticleApi;
import com.wnxy.common.base.Result;
import com.wnxy.replies.dto.RepliesDto;
import com.wnxy.replies.entity.Replies;
import com.wnxy.replies.service.RepliesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/replies")
@Api(tags = "回复模块")
public class RepliesController {

    @Autowired
    private RepliesService repliesService;
    @Autowired
    private ArticleApi articleApi;

    /**
     * 1、保存回复
     */
    @PostMapping
    @ApiOperation("保存回复")
    public Result save(@RequestBody @Validated RepliesDto repliesDto) {
        //1.1 保存回复
        Replies replies = new Replies();
        BeanUtils.copyProperties(repliesDto,replies);
        repliesService.save(replies);
        //1.2 修改文章回复数量 (open feign)
        articleApi.updateInfosReplyCount(repliesDto.getInfoId());
        return Result.ok();
    }
}
